package com.test05_数组应用;

/**
 * 数据表与简单Java类
 */
public class YootkDemo_02 {

    public static void main(String[] args) {
        // 声明并实例化各自的类对象
        Dept dept = new Dept(10, "Yootk教学部", "北京");
        Emp empA = new Emp(7367, "Smith", "CLERK", 800.0, 0.0);
        Emp empB = new Emp(7368, "Ford", "MANAGER", 2450.0, 0.0);
        Emp empC = new Emp(7369, "King", "PRESIDENT", 5000.0, 0.0);
        // 要设置彼此之间的引用关联
        empA.setDept(dept);
        empB.setDept(dept);
        empC.setDept(dept);
        empA.setMgr(empB);
        empB.setMgr(empC);
        dept.setEmps(new Emp[]{empA, empB, empC});
        // 依据引用结构获取相关的数据信息
        System.out.println(dept.getInfo());
        for (int x = 0; x < dept.getEmps().length; x++) {
            System.out.println("\t|- " + dept.getEmps()[x].getInfo());
            if (dept.getEmps()[x].getMgr() != null) {
                System.out.println("\t\t|- " + dept.getEmps()[x].getMgr().getInfo());
            }
        }
    }
}

/**
 * 部门类
 */
class Dept {
    private long deptno;
    private String dname;
    private String loc;
    private Emp[] emps;     // 保存多个雇员

    public Dept() {
    }

    public Dept(long deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public void setEmps(Emp emps[]) {
        this.emps = emps;
    }

    public Emp[] getEmps() {
        return this.emps;
    }

    // setter、getter略 ...
    public String getInfo() {
        return "【Dept】部门编号：" + this.deptno + "、部门名称：" + this.dname + "、部门位置：" + this.loc;
    }
}

/**
 * 雇员类
 */
class Emp {
    private long empno;
    private String ename;
    private String job;
    private double sal;
    private double comm;
    private Dept dept;      // 部门
    private Emp mgr;        // 领导

    public Emp() {
    }

    public Emp(long empno, String ename, String job, double sal, double comm) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.sal = sal;
        this.comm = comm;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public Dept getDept() {
        return this.dept;
    }

    public void setMgr(Emp mgr) {
        this.mgr = mgr;
    }

    public Emp getMgr() {
        return this.mgr;
    }

    public String getInfo() {
        return "【Emp】雇员编号：" + this.empno + "、雇员姓名：" + this.ename + "、职位：" + this.job + "、工资：" + this.sal + "、佣金：" + this.comm;
    }
}
